package com.abc.controller;

import com.abc.service.ex.GlobalException;
import com.abc.service.ex.InsertException;
import com.abc.service.ex.ServiceException;
import com.abc.service.ex.UsernameDuplicateException;
import com.abc.util.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;

public class BaseController {
    @ExceptionHandler({ServiceException.class})
    public JsonResult<Void> handleException(Throwable e) {
        System.out.println(">>>"+e);
        JsonResult<Void> jr = new JsonResult<>();
        jr.setMessage(e.getMessage());

        if (e instanceof UsernameDuplicateException) {
            // 4000-用户名冲突异常，例如注册时用户名已经被占用
            jr.setState(4000);
        } else if (e instanceof InsertException) {
            // 5000-插入数据异常
            jr.setState(5000);
        } else if (e instanceof GlobalException) {
            // 5000-用户名或密码错误,登录失败
            jr.setState(6000);
        }
        return jr;
    }
}
